Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 购物车数据
    cartItems: [
      {
        image:"https://img.picui.cn/free/2025/05/12/6821d40d1ac34.jpg",
        name:"橘娃-淡黄小花裙",
        price: 79.9,
      },
      {
        image:"https://img.picui.cn/free/2025/05/12/6821d41033e7d.jpg",
        name:"橘娃-经典版",
        price: 79.9,
      },
      {
        image:"https://img.picui.cn/free/2025/05/12/6821d4135e201.jpg",
        name:"日历",
        price: 29.9,
      }
    ],
    // 商品分类、商品列表数组
    cateItems:[
      {
        cate_id:1,
        cate_name:"橘娃",
        children: [
          {
            nencode:"001",
            name: "橘娃-淡黄小花裙",
            image: "https://img.picui.cn/free/2025/05/12/6821d40d1ac34.jpg" ,
            price: 79.9,
            storeCount: 23,  
          },{ 
            nencode:"002",
            name: "橘娃-经典版",
            image: "https://img.picui.cn/free/2025/05/12/6821d41033e7d.jpg" ,
            price: 79.9,
            storeCount: 55,  
          },{ 
            nencode:"003",
            name: "橘娃-粉红连衣小花裙",
            image: "https://img.picui.cn/free/2025/05/12/6821d4103128c.jpg" ,
            price: 79.9,
            storeCount: 32,  
          },{  
            nencode:"004",
            name: "橘娃-粉红小花裙",
            image: "https://img.picui.cn/free/2025/05/12/6821d40eaa90f.jpg" ,
            price: 79.9,
            storeCount: 11,
          },{ 
            nencode:"005",
            name: "橘娃-手工兔",
            image: "https://img.picui.cn/free/2025/05/12/6821d40c7888c.jpg" ,
            price: 119.9,
            storeCount: 12,
          },{ 
            nencode:"020",
            name: "橘娃-特别版青丝",
            image: "https://img.picui.cn/free/2025/05/12/6821d40444546.jpg" ,
            price: 119.9,
            storeCount: 11,
          },{ 
            nencode:"021",
            name: "橘娃-特别版棕发",
            image: "https://img.picui.cn/free/2025/05/12/6821d408554f8.jpg" ,
            price: 119.9,
            storeCount: 5,
          }
        ]
      },
      {
        cate_id:2,
        cate_name:"周边",
        children: [
          { 
            nencode:"014",
            name: "橘娃贴纸",
            image: "https://img.picui.cn/free/2025/05/12/6821d40bca09d.jpg",
            price: 12.9,
            storeCount: 15,
          },
          {
              nencode:"006",
              name: "橘娃小兔",
              image: "https://img.picui.cn/free/2025/05/12/6821d421efe7f.jpg",
              price: 49.9,
              storeCount: 34,
          }, 
          {
            nencode:"007",
            name: "帆布包",
            image: "https://img.picui.cn/free/2025/05/12/6821d417d21f3.jpg",
            price: 39.9,
            storeCount: 45,  
          },
          {
            nencode:"008",
            name: "礼品盒",
            image: "https://img.picui.cn/free/2025/05/12/6821d420e68ba.jpg",
            price: 20.0,
            storeCount: 33,  
          },{
            nencode:"009",
            name: "水杯",
            image: "https://img.picui.cn/free/2025/05/12/6821d410cf7c2.jpg",
            price: 35.0,
            storeCount: 22,  
          },{
            nencode:"010",
            name: "手机壳",
            image: "https://img.picui.cn/free/2025/05/12/6821d40894c10.jpg",
            price:18.8,
            storeCount: 31,  
          },{
            nencode:"011",
            name: "日历",
            image: "https://img.picui.cn/free/2025/05/12/6821d4135e201.jpg" ,
            price: 29.9,
            storeCount: 43,  
          },{
            nencode:"012",
            name: "抱枕",
            image: "https://img.picui.cn/free/2025/05/12/6821d418a39fd.jpg" ,
            price:50.0,
            storeCount: 12,  
          },{
            nencode:"013",
            name: "胶带",
            image: "https://img.picui.cn/free/2025/05/12/6821d41ca775b.jpg" ,
            price:22.4,
            storeCount: 54,  
          },{
            nencode:"015",
            name: "徽章",
            image: "https://img.picui.cn/free/2025/05/12/6821d40915153.jpg" ,
            price:18.8,
            storeCount: 21,  
          },{
            nencode:"019",
            name: "橘娃挂件",
            image: "https://img.picui.cn/free/2025/05/12/6821d413af3d0.jpg" ,
            price:18.8,
            storeCount: 55,  
          }
        ]
      },
      {
        cate_id:3,
        cate_name:"纪念",
        children: [
          {
            nencode:"016",
            name: "元通古塔",
            image: "https://img.picui.cn/free/2025/05/12/6821d41e9351f.jpg",
            price:188.8,
            storeCount: 33,  
          },
          {
            nencode:"017",
            name: "元通币",
            image: "https://img.picui.cn/free/2025/05/12/6821d41112679.jpg" ,
            price:45.5,
            storeCount: 35,  
          },{
            nencode:"018",
            name: "元宝",
            image: "https://img.picui.cn/free/2025/05/12/6821d4212bee8.jpg" ,
            price:35.5,
            storeCount: 13,  
          }
        ]
      }
    ],
    curNav:1,
    curIndex:0,
    showCartPopup: false, // 控制购物车弹窗显示或隐藏
    cartItemCount: 0, // 购物车中商品数量
    totalPrice: 0, // 添加总价字段
  },
  // 点击购物车图标，显示或隐藏购物车弹窗
  toggleCart: function () {
    // 每次打开购物车时重新获取最新数据
    var cartItems = wx.getStorageSync("cartItems") || [];
    this.setData({
      showCartPopup: !this.data.showCartPopup,
      cartItems: cartItems,
      cartItemCount: cartItems.length
    });
  },
  // 竖版导航
  switchRightTab:function(e){
    let id = e.target.dataset.id,index=e.target.dataset.index;
    this.setData({
      curNav:id,
      curIndex:index
    })
  },
  // 点击事件函数
  goToDetail: function(event) {
    var nencode = event.currentTarget.dataset.nencode;
    wx.navigateTo({
      url: "/packageA/pages/Product_details/Product_details?nencode=" + nencode
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 从本地存储获取购物车数据
    var cartItems = wx.getStorageSync("cartItems") || [];
    
    // 更新购物车数据
    this.setData({
      cartItems: cartItems,
      cartItemCount: cartItems.length
    });
    this.calculateTotal();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 每次页面显示时更新购物车数据
    var cartItems = wx.getStorageSync("cartItems") || [];
    this.setData({
      cartItems: cartItems,
      cartItemCount: cartItems.length
    });
    this.calculateTotal();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // 点击删除按钮，删除购物车中的商品
  deleteItem: function (event) {
    var index = event.currentTarget.dataset.index;
    var cartItems = this.data.cartItems;
    cartItems.splice(index, 1);
    // 更新本地存储
    wx.setStorageSync("cartItems", cartItems);
    this.setData({
      cartItems: cartItems,
      cartItemCount: cartItems.length
    });
  },
  // 计算总价
  calculateTotal: function() {
    const total = this.data.cartItems.reduce((sum, item) => {
      return sum + (item.price * item.quantity);
    }, 0);
    this.setData({
      totalPrice: total.toFixed(2)
    });
  },

  // 增加商品数量
  increaseQuantity: function(e) {
    const index = e.currentTarget.dataset.index;
    let cartItems = this.data.cartItems;
    cartItems[index].quantity = (cartItems[index].quantity || 1) + 1;
    this.setData({
      cartItems: cartItems
    });
    this.calculateTotal();
    wx.setStorageSync("cartItems", cartItems);
  },

  // 减少商品数量
  decreaseQuantity: function(e) {
    const index = e.currentTarget.dataset.index;
    let cartItems = this.data.cartItems;
    if (cartItems[index].quantity > 1) {
      cartItems[index].quantity = cartItems[index].quantity - 1;
      this.setData({
        cartItems: cartItems
      });
      this.calculateTotal();
      wx.setStorageSync("cartItems", cartItems);
    }
  },

  // 结算
  checkout: function() {
    if (this.data.cartItems.length === 0) {
      wx.showToast({
        title: "购物车为空",
        icon: "none"
      });
      return;
    }

    // 生成订单
    const order = {
      id: "ORDER" + Date.now(),
      items: this.data.cartItems,
      totalPrice: this.data.totalPrice,
      status: "toPay",
      statusText: "待付款",
      createTime: new Date().toLocaleString()
    };

    // 获取现有订单
    let orders = wx.getStorageSync("orders") || [];
    orders.unshift(order);
    wx.setStorageSync("orders", orders);

    // 清空购物车
    wx.setStorageSync("cartItems", []);
    this.setData({
      cartItems: [],
      cartItemCount: 0,
      showCartPopup: false,
      totalPrice: 0
    });

    wx.showToast({
      title: "下单成功",
      icon: "success",
      duration: 2000,
      success: () => {
        // 跳转到订单页面
        setTimeout(() => {
          wx.navigateTo({
            url: "/pages/orders/orders?status=toPay"
          });
        }, 2000);
      }
    });
  },
})
